package com.laiketui.mch.controller;

import com.laiketui.common.mapper.MchModelMapper;
import com.laiketui.common.utils.tool.cache.RedisDataTool;
import com.laiketui.core.cache.RedisUtil;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.domain.lktconst.DictionaryConst;
import com.laiketui.domain.lktconst.ErrorCode;
import com.laiketui.domain.mch.MchModel;
import com.laiketui.domain.user.User;
import com.laiketui.domain.vo.MainVo;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;

public class MchBaseController {
    @Autowired
    MchModelMapper mchModelMapper;
    @Autowired
    private RedisUtil redisUtil;

    public MchModel currentMch(MainVo params){
        User user = RedisDataTool.getRedisUserCache(params.getAccessId(), redisUtil, GloabConst.RedisHeaderKey.LOGIN_ACCESS_MCH_TOKEN, true);
//        User user = testUser();

        MchModel mchModel = new MchModel();
        mchModel.setStore_id(user.getStore_id());
        mchModel.setId(user.getMchId());
//        mchModel.setUser_id(user.getUser_id());
        mchModel.setRecovery(DictionaryConst.ProductRecycle.NOT_STATUS);
        mchModel = mchModelMapper.selectOne(mchModel);
        if (mchModel == null) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_FFRQ, "非法入侵");
        }
        return mchModel;
    }

    private static @NotNull User testUser() {
        User user = new User();
        user.setUser_id("user829");
        user.setStore_id(1);
        user.setRole_id(379);
        user.setMobile("13800000000");
        user.setUser_name("百货自营");
        user.setMchId(193);
        return user;
    }
}
